<?php
/**
 * Created by PhpStorm.
 * User: zhenghao
 * Date: 2015/6/29
 * Time: 10:51
 */

class NewsBO {


    //查询一组产品分类下的新闻列表（取时间最新的前5条记录）
    static function loveNews($type_ids){

        global $conf;

        //通过产品类型编号，获取最新前5条新闻编号
        $table_name=$conf["DB_PREFIX"]."news_product";

        $str=array(
            "fields" => "DISTINCT news_id",
            "where" => "product_type_id in (".$type_ids.") and `disable`=0",
            "order" => "renew_date DESC",
            "count" => 5
        );

        $db=new MyAdodb();

        $news_ids=$db->select($table_name,$str);

        if(empty($news_ids)) return null;

        //按编号查询新闻详情,处理新闻编号
        $str_ids="";

        foreach($news_ids as $key=>$value){

            $str_ids.=$value["news_id"].",";
        }

        if(!empty($str_ids)) $str_ids=substr($str_ids,0,strlen($str_ids)-1);

        //执行查询
        $table_name=$conf["DB_PREFIX"]."news_title";

        $str=array(
            "fields" => "news_id,title_name,pub_date",
            "where" => "news_id in (".$str_ids.")",
            "order" => "renew_date DESC",
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        return $result;
    }

    //获取指定新闻的相关新闻
    static function relatedNews($id){

        //获取包含指定新闻关键字的新闻编号列表，取时间最新的前5个
        $id_list=self::getRelatedKeywordsNewsById($id,5);

        if(empty($id_list)) return null;

        //处理新闻编号，转换为字符串
        $str_ids="";

        foreach($id_list as $key=>$value){

            $str_ids.=$value["news_id"].",";
        }

        if(!empty($str_ids)) $str_ids=substr($str_ids,0,strlen($str_ids)-1);

        //按指定编号，查询新闻标题
        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_title";

        $str=array(
            "fields" => "news_id,title_name,pub_date",
            "where" => "news_id in (".$str_ids.")",
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        return $result;
    }


    //获取包含相关关键词的新闻列表
    private static function getRelatedKeywordsNewsById($id,$count){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_rekeyword a JOIN (select news_keyword_id
                from ".$conf["DB_PREFIX"]."news_rekeyword
                where news_id=".$id." and disable=0) b on a.news_keyword_id=b.news_keyword_id";

        $str=array(
            "fields" => "a.news_id",
            "order" => "a.renew_date DESC",
            "count" => $count
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        return $result;
    }



    //获取指定新闻的关键词列表
    static function keywordsByNewsId($id){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_rekeyword a
                join ".$conf["DB_PREFIX"]."news_keyword b on a.news_keyword_id=b.news_keyword_id";

        $str=array(
            "fields" => "b.news_keyword_id,b.news_keyword_name",
            "where" => "a.news_id=".$id,
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        return $result;
    }


    //查询新闻详情
    static function info($id){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_title a
                join ".$conf["DB_PREFIX"]."news_content b on a.news_id=b.news_id";

        $str=array(
            "fields" => "a.news_id,a.title_name,b.news_content,b.renew_date,a.come_from",
            "where" => "a.news_id=".$id." and a.`disable`=0 and a.`language`='cn'",
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        if(empty($result)) return null;

        return $result[0];
    }


    //统计指定分类下新闻总数量
    static function countByType($type_id){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_title a
                join (select nid from smm_news_type where parent_id='".$type_id."') b on a.type_id=b.nid";

        $str=array(
            "fields" => "count(a.news_id) as number",
            "where" => "a.`disable`=0 and a.`language`='cn'",
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        if(empty($result)) return 0;

        return $result[0]["number"];
    }


    //统计指定品类下新闻总数量
    static function countByProductType($type_id){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_title a
                JOIN ".$conf["DB_PREFIX"]."news_product b on a.news_id=b.news_id";

        $str=array(
            "fields" => "count(a.news_id) as number",
            "where" => "b.product_type_id=".$type_id." and a.`disable`=0 and a.`language`='cn'",
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        if(empty($result)) return 0;

        return $result[0]["number"];

    }


    //获取指定分类下新闻列表（发布时间倒叙，分页显示）
    static function listByType($type_id,$page){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_title a join
        (select nid,news_type_name from ".$conf["DB_PREFIX"]."news_type where parent_id='".$type_id."') b
        on a.type_id=b.nid JOIN ".$conf["DB_PREFIX"]."news_content c on a.news_id=c.news_id";

        $str=array(
            "fields" => "a.news_id,b.nid as type_id,b.news_type_name,a.title_name,c.news_profile,a.renew_date,a.is_free",
            "where" => "a.`disable`=0 and a.`language`='cn'",
            "order" => "a.renew_date DESC"
        );

        $db=new MyAdodb();

        $result=$db->get_limit($table_name,$str,$page->one_page_rows,$page->limit_start);

        return $result;
    }


    //获取指定品类下新闻列表（发布时间倒叙，分页显示）
    static function listByProductType($type_id,$page){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."news_title a
                    JOIN ".$conf["DB_PREFIX"]."news_product b on a.news_id=b.news_id
                    join ".$conf["DB_PREFIX"]."news_content c on a.news_id=c.news_id
                    JOIN ".$conf["DB_PREFIX"]."news_type d on a.type_id=d.nid";

        $str=array(
            "fields" => "a.news_id,a.title_name,c.news_profile,a.renew_date,a.is_free ,b.product_type_id,d.news_type_name",
            "where" => "b.product_type_id=".$type_id." and a.`disable`=0 and a.`language`='cn'",
            "order" => "a.renew_date DESC"
        );

        $db=new MyAdodb();

        $result=$db->get_limit($table_name,$str,$page->one_page_rows,$page->limit_start);

        return $result;

    }

}